﻿package mortal.game.view.guildNew.manor
{
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.mvc.core.*;

    public class GuildNewManorPanel extends GSprite implements GuildNewPanelI
    {
        protected var _topBg:GImageBitmap;
        protected var _bottomBg:ScaleBitmap;
        protected var _listTitleBg:ScaleBitmap;
        protected var _tOther:GTextFiled;
        protected var _tMor:GTextFiled;
        protected var tLevel:GTextFiled;
        protected var _tfruit:GTextFiled;
        protected var _tFrui2:GTextFiled;
        protected var _tOper:GTextFiled;
        protected var _list:GTileList;
        protected var _tipTxt:GTextFiled;
        protected var _myGuildMatTime:GTextFiled;
        protected var _enterBtn:GLoadedButton;
        protected var _pageSelecter:PageSelecter;
        protected var _txtMyGuildRank:GTextFiled;
        protected var _txtMyGuild:GButton;
        protected var _btnInviteList:GButton;
        protected var _NullGuildTxt:GTextFiled;
        private var _comboBoxFilterCamp:GComboBox;
        private var _secT:SecTimerView;
        private var _selectMy:Boolean = false;
        private static const PAGE_CELL_COUNT:int = 5;

        public function GuildNewManorPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.guildNManor, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            this._topBg = UIFactory.gImageBitmap(ImagesConst.GuildNewManorBg, 0, 0, this);
            this._bottomBg = UIFactory.bg(-2, 225, 747, 250, this, ImagesConst.PanelBg);
            this._listTitleBg = UIFactory.bg(-2, 227, 750, 23, this, ImagesConst.RegionTitleBg);
            this._tOther = UIFactory.gTextField("" + Language.getString(71484), 58, 227, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._tMor = UIFactory.gTextField("" + Language.getString(71485), 204, 227, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this.tLevel = UIFactory.gTextField("" + Language.getString(71486), 311, 227, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._tfruit = UIFactory.gTextField("" + Language.getString(71487), 410, 228, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._tFrui2 = UIFactory.gTextField("" + Language.getString(71488), 462, 228, 120, 25, this, GlobalStyle.textFormatHuang2);
            this._tOper = UIFactory.gTextField("" + Language.getString(71489), 654, 227, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._list = UIFactory.tileList(9, 249, 762, 170, this);
            this._list.direction = GBoxDirection.VERTICAL;
            this._list.verticalGap = 0;
            this._list.horizontalGap = 2;
            this._list.rowHeight = 32;
            this._list.columnWidth = 747;
            this._list.columnCount = 1;
            this._list.rowCount = 5;
            this._list.width = 762;
            this._list.setStyle("cellRenderer", GuildNewManorCell);
            this._tipTxt = UIFactory.gTextField("" + Language.getString(71490), 0, 443, 748, 20, this, GlobalStyle.textFormatLv.setSize(13).center());
            this._myGuildMatTime = UIFactory.gTextField("", 293, 194, 170, 20, this, GlobalStyle.textFormatLv.center());
            this._enterBtn = UIFactory.gLoadedButton(ImagesConst.GuildNManorEnter_upSkin, 320, 156, 111, 38, this);
            this._enterBtn.label = "";
            this._pageSelecter = UIFactory.pageSelecter(319, 415, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, GlobalStyle.textFormatBai.center());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._txtMyGuildRank = UIFactory.gTextField("" + Language.getString(71491), 79, 441, 100, 20, this);
            this._txtMyGuild = UIFactory.gButton("" + Language.getString(71492), 9, 435, 80, 30, this, "GreenLinkButton");
            this._btnInviteList = UIFactory.gButton("" + Language.getString(71493), 632, 432, 70, 26, this);
            this._NullGuildTxt = UIFactory.gTextField("" + Language.getString(71494), 332, 320, 100, 20, this);
            this._myGuildMatTime.text = "";
            this._enterBtn.configEventListener(MouseEvent.CLICK, this.onEnterManorHd);
            this._txtMyGuild.configEventListener(MouseEvent.CLICK, this.onTextLink);
            var _loc_1:Array = [{label:Language.getString(60053), type:0}, {label:GameDefConfig.instance.getItem("ECamp", 1).text, type:1}, {label:GameDefConfig.instance.getItem("ECamp", 2).text, type:2}, {label:GameDefConfig.instance.getItem("ECamp", 3).text, type:3}];
            var _loc_2:* = new DataProvider(_loc_1);
            this._comboBoxFilterCamp = UIFactory.gComboBox(64, 40, 80, 22, _loc_2, this);
            this._comboBoxFilterCamp.configEventListener(Event.CHANGE, this.onCampSelectChange);
            this._comboBoxFilterCamp.visible = false;
            var _loc_3:* = GlobalStyle.textFormatBai;
            _loc_3.align = "center";
            this._btnInviteList.configEventListener(MouseEvent.CLICK, this.onInviteList);
            this._NullGuildTxt.htmlText = "<font size=\'22\'>" + Language.getString(71495) + "</font>";
            this._NullGuildTxt.visible = false;
            this.getDataFromServer();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._selectMy = false;
            LoaderHelp.removeResEvent(ResFileConst.guildNManor, this.onResLoad);
            this._topBg = null;
            this._bottomBg = null;
            this._listTitleBg = null;
            this._tOther = null;
            this._tMor = null;
            this.tLevel = null;
            this._tfruit = null;
            this._tFrui2 = null;
            this._tOper = null;
            this._list = null;
            this._tipTxt = null;
            this._myGuildMatTime = null;
            this._enterBtn = null;
            this._pageSelecter = null;
            this._txtMyGuildRank = null;
            this._txtMyGuild = null;
            this._btnInviteList = null;
            this._NullGuildTxt = null;
            this._secT = null;
            this._comboBoxFilterCamp = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function onCampSelectChange(event:Event) : void
        {
            return;
        }// end function

        private function updateFruiteTime() : void
        {
            var _loc_1:Date = null;
            var _loc_2:Date = null;
            var _loc_3:int = 0;
            if (Cache.instance.guild.selfGuildInfo.gardenInfo == null)
            {
                return;
            }
            if (GuildUtil.canGetFruit())
            {
                this._myGuildMatTime.text = "" + Language.getString(71496);
            }
            else
            {
                if (this._secT == null)
                {
                    this._secT = UIFactory.secTimeView("hh:mm:ss", 0, 0);
                    this._secT.configEventListener(EventName.SecViewTimeChange, this.onTimerHandler);
                }
                _loc_1 = ClockManager.instance.nowDate;
                _loc_2 = Cache.instance.guild.selfGuildInfo.gardenInfo.nextPickUpDt;
                _loc_3 = (_loc_2.time - _loc_1.time) / 1000;
                this._secT.setLeftTime(_loc_3);
            }
            return;
        }// end function

        private function onTimerHandler(event:DataEvent) : void
        {
            this._myGuildMatTime.text = Language.getString(71497) + this._secT.text;
            return;
        }// end function

        private function onEnterManorHd(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_enterManor, _loc_2.baseInfo.guildId));
            return;
        }// end function

        private function getDataFromServer() : void
        {
            this._comboBoxFilterCamp.selectedIndex = 0;
            GuildNewUtil.getManorInfo();
            return;
        }// end function

        private function onInviteList(event:MouseEvent) : void
        {
            if (Cache.instance.guild.inviteList.length == 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71498));
                return;
            }
            GuildInviteListPanel.instance.popUp();
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            var _loc_2:* = this._pageSelecter.currentPage;
            var _loc_3:* = (_loc_2 - 1) * PAGE_CELL_COUNT;
            var _loc_4:* = new Object();
            _loc_4.camp = this._comboBoxFilterCamp.selectedItem == null ? (0) : (this._comboBoxFilterCamp.selectedItem.type);
            _loc_4.guildName = "";
            _loc_4.startIdx = _loc_3;
            _loc_4.isfull = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_4));
            return;
        }// end function

        public function onTextLink(event:MouseEvent) : void
        {
            this._selectMy = true;
            if (this._comboBoxFilterCamp == null || Cache.instance.guild.selfGuildInfo == null)
            {
                return;
            }
            if (this._comboBoxFilterCamp.selectedItem.type == Cache.instance.guild.selfGuildInfo.baseInfo.camp || this._comboBoxFilterCamp.selectedItem.type == 0)
            {
                this._pageSelecter.currentPage = Math.ceil(Cache.instance.guild.selfGuildInfo.baseInfo.rank / PAGE_CELL_COUNT);
                this.onPageChange(null);
            }
            return;
        }// end function

        public function update() : void
        {
            var _loc_7:SMiniGuildInfo = null;
            if (!Cache.instance.guild.guildList)
            {
                return;
            }
            var _loc_1:* = Cache.instance.guild;
            this._pageSelecter.maxPage = _loc_1.guildTotalCount / PAGE_CELL_COUNT == 0 ? (1) : (Math.ceil(_loc_1.guildTotalCount / PAGE_CELL_COUNT));
            var _loc_2:* = this._pageSelecter.currentPage;
            var _loc_3:* = (_loc_2 - 1) * PAGE_CELL_COUNT;
            var _loc_4:* = new DataProvider();
            var _loc_5:* = _loc_1.guildList.length;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5 && _loc_6 < PAGE_CELL_COUNT)
            {
                
                if (_loc_6 >= _loc_1.guildList.length)
                {
                    break;
                }
                _loc_7 = _loc_1.guildList[_loc_6];
                _loc_4.addItem(_loc_7);
                _loc_6++;
            }
            this._list.dataProvider = _loc_4;
            this._list.drawNow();
            if (_loc_5 > 0)
            {
                this._NullGuildTxt.visible = false;
            }
            else
            {
                this._NullGuildTxt.visible = true;
            }
            var _loc_8:Boolean = true;
            this._txtMyGuildRank.visible = true;
            this._txtMyGuild.visible = _loc_8;
            this._txtMyGuildRank.htmlText = "<font color=\'#ffffff\'>" + Language.getString(60052) + " : </font><font color=\'#ffcc00\'>" + Cache.instance.guild.selfGuildInfo.baseInfo.rank + "</font>";
            if (this._selectMy)
            {
                this.onTextLink(null);
                this._selectMy = false;
            }
            this.updateFruiteTime();
            return;
        }// end function

    }
}
